package com.gloaba.resource.feign;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import com.gloaba.common.core.domain.R;
import com.gloaba.common.core.exception.CustomException;
import com.gloaba.common.core.web.controller.BaseController;
import com.gloaba.common.security.annotation.InnerAuth;
import com.gloaba.resource.api.domain.SysFile;
import com.gloaba.resource.api.domain.UploadFileVo;
import com.gloaba.resource.api.service.FileRemoteService;
import com.gloaba.resource.domain.vo.SysOssVo;
import com.gloaba.resource.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequiredArgsConstructor
@RestController
public class RemoteFileController extends BaseController implements FileRemoteService {

    private final ISysOssService iSysOssService;

    @InnerAuth
    public R<SysFile> upload(UploadFileVo uploadFileVo)  {
        if (ObjectUtil.isNull(uploadFileVo.getBase64Data())) {
            throw new CustomException("上传文件不能为空");
        }
        if (ObjectUtil.isNull(uploadFileVo.getFileName())) {
            throw new CustomException("上传文件名不能为空");
        }
        SysOssVo oss = iSysOssService.upload(uploadFileVo.getFileName(), uploadFileVo.getContentType(), uploadFileVo.getPath(), Base64.decode(uploadFileVo.getBase64Data()),uploadFileVo.getUseOriginalFileName());
        SysFile sysFile = new SysFile();
        sysFile.setFileUrl(oss.getUrl());
        sysFile.setFileName(oss.getOriginalName());
        sysFile.setPath(oss.getFileName());
        sysFile.setOssId(oss.getId());
        return R.ok(sysFile);
    }

}
